﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using S9Engine;

public partial class Print : Parser
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            _parser.SetCurrentDir("~/tpl");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);

            Session["CurrentPage"] = "News.aspx";

            LoadFile("print.html", false);

            LoadNewsByID(_nid);
            _parser.Assign("currentCategoryID", Request["cid"].ToString());
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadNewsByID(long ID)
    {
        if (ID == 0) return;

        //Posts post = _DR.Posts.Where(p => p.LanguageCode.Equals(_currentLanguage) && p.ID.Equals(ID)).FirstOrDefault();

        var post = (from term in _DR.Terms.Where(t => t.LanguageCode.Equals(_currentLanguage))
                    join taxonomy in _DR.Taxonomy.Where(t => t.Name.Equals("news-category")) on term.ID equals taxonomy.Term
                    join relation in _DR.Relations.Where(r => r.Type.Equals("news-category")) on term.ID equals relation.Parent
                    join pos in _DR.Posts.Where(p => p.Type.Equals("news") && p.ID.Equals(ID)) on relation.Child equals pos.ID
                    join metaPostAuthor in _DR.Meta.Where(m => m.Key.Equals("Author")) on pos.ID equals metaPostAuthor.Object into Authorfun
                    from mAuthor in Authorfun.DefaultIfEmpty()
                    //join metaPostDocument in _DR.Meta.Where(m => m.Key.Equals("Document")) on pos.ID equals metaPostDocument.Object into Documentfun
                    //from mDocument in Documentfun.DefaultIfEmpty()
                    select new
                    {
                        ID = pos.ID,
                        Title = pos.Title,
                        Content = pos.Content,
                        Author = mAuthor.Value,
                        //Document = mDocument.Value,
                        CategoryID = term.ID
                    }).FirstOrDefault();

        if (post == null) { _parser.Parse(string.Format("Main.Noresult")); return; }

        _parser.Assign("newsID", post.ID.ToString());
        _parser.Assign("newsTitle", post.Title);
        _parser.Assign("newsContent", AssignWebCmsPath(post.Content));
        _parser.Assign("newsAuthor", string.IsNullOrEmpty(post.Author) ? "Đang cập nhật" : post.Author);
        //_parser.Assign("newsDocument", string.IsNullOrEmpty(post.Document) ? "#" : _webcmsPath + post.Document);
        _parser.Assign("categoryID", post.CategoryID.ToString());

        _parser.Parse("Main.News");
    }

    private void SamePost(long ID, int noOfNews)
    {
        if (ID == 0) return;

        var posts = (from term in _DR.Terms.Where(t => t.LanguageCode.Equals(_currentLanguage) && t.ID.Equals(ID))
                     join relation in _DR.Relations.Where(r => r.Type.Equals("news-category")) on term.ID equals relation.Parent
                     join post in _DR.Posts on relation.Child equals post.ID
                     select new
                     {
                         ID = post.ID,
                         Title = post.Title,
                         Date = post.Date,
                         CategoryID = term.ID
                     }).OrderByDescending(p => p.Date).ThenByDescending(p => p.ID).ToList();
        posts = posts.Where(p => !p.ID.Equals(_nid)).ToList();
        if (noOfNews != 0) posts = posts.Take(noOfNews).ToList();
        if (posts.Count == 0) return;
        for (int j = 0; j < posts.Count; ++j)
        {
            _parser.Assign("newsID", posts[j].ID.ToString());
            _parser.Assign("newsTitle", posts[j].Title);
            _parser.Assign("newsCategoryID", posts[j].CategoryID.ToString());
            //string time = ZeroNumber(posts[j].Date.Day) + "/" + ZeroNumber(posts[j].Date.Month) + "/" + posts[j].Date.Year.ToString();
            //_parser.Assign("newsDate", time);

            _parser.Parse(string.Format("Main.SameNews.SameNewsItem"));
        }
        _parser.Parse(string.Format("Main.SameNews"));
    }
}